﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentQuake : ComponentBehavior, IUpdateable
    {
        public SubsystemParticles m_subsystemParticles;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemPlayers m_subsystemPlayers;

        public ComponentCreature m_componentCreature;

        public ComponentHealth m_componentHealth;

        public SubsystemTime m_subsystemTime;

        public new StateMachine m_stateMachine = new StateMachine();

        public override float ImportanceLevel => 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            ((Component)this).Load(valuesDictionary, idToEntityMap);
            m_componentCreature = base.Entity.FindComponent<ComponentCreature>(throwOnError: true);
            m_componentHealth = base.Entity.FindComponent<ComponentHealth>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemPlayers = base.Project.FindSubsystem<SubsystemPlayers>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
        }

        public void Update(float dt)
        {
            foreach (ComponentPlayer componentPlayer in m_subsystemPlayers.ComponentPlayers)
            {
                float num = Vector3.Distance(m_componentCreature.ComponentBody.Position, componentPlayer.ComponentBody.Position);
                if (componentPlayer != null && m_subsystemTime.PeriodicGameTimeEvent(3.0, -0.01))
                {
                    componentPlayer.ComponentGui.DisplaySmallMessage("附近有大型动物游荡，你感觉到剧烈的震动...", Color.White, blinking: false, playNotificationSound: false);
                }

                double period = (double)num / 8.0;
                if (componentPlayer != null && m_subsystemTime.PeriodicGameTimeEvent(period, -0.01))
                {
                    float num2 = MathUtils.DegToRad(MathUtils.Lerp(-35f, -60f, SimplexNoise.Noise(2f * (float)MathUtils.Remainder(m_subsystemTime.GameTime, 10000.0))));
                    Vector3 upVector = m_componentCreature.ComponentCreatureModel.EyeRotation.GetUpVector();
                    Vector3 forwardVector = m_componentCreature.ComponentCreatureModel.EyeRotation.GetForwardVector();
                    componentPlayer.ComponentBody.ApplyImpulse(-2f * forwardVector);
                }
            }

            if (m_subsystemTime.PeriodicGameTimeEvent(1.5, -0.01))
            {
                m_subsystemParticles.AddParticleSystem(new KillParticleSystem(m_subsystemTerrain, m_componentCreature.ComponentBody.Position, 2f));
            }
        }
    }
}